package com.test.pdproject.controller;

import com.qiniu.api.auth.AuthException;
import com.test.pdproject.config.ConstantQiniu;
import com.test.pdproject.service.qiniustore.QiniuMediaUtilService;
import com.test.pdproject.util.AppResponse;
import com.test.pdproject.util.Tools;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @ClassName QiNiuController
 * @Descirption TODO
 * @Author Administrator
 * @Date 七牛云上传控制类 15:14
 * @Version 1.0
 **/
@Controller
@RequestMapping("/admin/yxbb")
public class QiNiuController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ConstantQiniu constantQiniu;
    @Autowired
    QiniuMediaUtilService qiniuMediaUtilService;
    /**
     * @Author lisaisai
     * @Description  上传图片文件
     * @Date 9:54 2018/9/14
     * @Param [multipartFile]
     * @return com.test.pdproject.util.AppResponse
     **/
    @PostMapping("/qiniu")
    @ResponseBody
    public AppResponse uploadImgQiniu(@RequestParam("file") MultipartFile multipartFile) throws IOException {

        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
        String path = qiniuMediaUtilService.uploadQNfile(inputStream, Tools.getRandomString("jpg")); // KeyUtil.genUniqueKey()生成图片的随机名
        return AppResponse.success(path);
    }
    /**
     * @Author lisaisai
     * @Description 上传视频文件
     * @Date 9:53 2018/9/14
     * @Param [multipartFile]
     * @return com.test.pdproject.util.AppResponse
     **/
    @PostMapping("/upload/video")
    @ResponseBody
    public AppResponse uploadVideoQiniu(@RequestParam("file") MultipartFile multipartFile) throws IOException, AuthException, JSONException {
        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
        String path = qiniuMediaUtilService.uploadQNfile(inputStream, Tools.getRandomString("flv")); // KeyUtil.genUniqueKey()生成图片的随机名
        return AppResponse.success(path);
    }
    @PostMapping("/upload/test")
    @ResponseBody
    public AppResponse uploadVideoQiniuTest(@RequestParam("file") MultipartFile multipartFile) throws IOException, AuthException, JSONException {
        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
        String path = qiniuMediaUtilService.uploadFile(Tools.getRandomString("flv"),inputStream); // KeyUtil.genUniqueKey()生成图片的随机名
        return AppResponse.success(path);
    }

    @PostMapping("/delete/test")
    @ResponseBody
    public AppResponse deleteFile(String fileName) throws IOException, AuthException, JSONException {
        // KeyUtil.genUniqueKey()生成图片的随机名
        try {
            qiniuMediaUtilService.delete(fileName);
            return AppResponse.success();
        } catch (Exception e) {
            logger.error("删除文件失败"+e.getMessage());
        }

        return AppResponse.error("删除文件失败");
    }

}
